package com.actions;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.beans.*;
import com.DAO.*;

public class AddNoticeAction extends org.apache.struts.action.Action{

	 private final static String SUCCESS = "success";
	  
	    
	    public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response)
	            throws Exception {
	    	
	    	PrintWriter out=response.getWriter();
	    	
	    	System.out.println("I am in action.......");
	    	
	    	NoticeForm noticeForm =(NoticeForm)form;
			
	    	String categoryName=noticeForm.getCategoryName();
			
	    	CategoryDAO catDAO=new CategoryDAO();
	    	NoticeDAO noticeDAO=new NoticeDAO();
	    	
	    	int categoryId=catDAO.getCategoryId(categoryName);
	    				
	    	noticeForm.setCategoryId(categoryId);
	    	
	    	HttpSession session=request.getSession();
	    	String username=session.getAttribute("name").toString();
	    	 	
			noticeForm.setCreatorName(username);
			
			String expdate=noticeForm.getExpirationDate();
			System.out.println("expire date before ..........."+expdate);
			String d[]=expdate.split("-");
			expdate=d[2]+"/"+d[1]+"/"+d[0];
			System.out.println("expire date after..........."+expdate);
			noticeForm.setExpirationDate(expdate);
			
			String userid=session.getAttribute("userid").toString();
			noticeForm.setCreatorId(userid);
			
			int rowsAffected=noticeDAO.insert(noticeForm);
			
			System.out.println("row affected="+rowsAffected);
			
			out.println("<script>alert('record inserted')</script>");
			System.out.println("after action.......");
			//noticeDao.closeConnection();
			return  mapping.findForward("success");
			
		} 
		
	
}
